-- Generated from template

-- print("Entering roshan killer game mode")

-- 服务器地址
local vpgame_server_address = "127.0.0.1:37012/test/"
-- 版本号
local version = "0.0.1"

-- 初始化
if RoshanKillerGameMode == nil then
	RoshanKillerGameMode = class({})
end

-- 载入json
require 'json'

function Precache( context )
end

function Activate()
	GameRules.RoshanKillerGameMode = RoshanKillerGameMode()
	GameRules.RoshanKillerGameMode:InitGameMode()
end

-- 当肉山被击杀
function RoshanKillerGameMode:OnRoshanKilled(keys)
	local time = GameRules:GetGameTime()
	local duration = time - self.flPreGameTime

	-- print(string.format("Roshan was killed, duration = %s", duration))

	-- 获取所有玩家的64位steamid
	local steamids = {}
	for i = 0, DOTA_MAX_PLAYERS do
		local player = PlayerResource:GetPlayer(i)
		if player and PlayerResource:IsValidTeamPlayer(i) then
			local steamid64 = tostring(PlayerResource:GetSteamID(i))
			table.insert(steamids, steamid64)
		end
	end

	-- print(JSON:encode(steamids))

	-- 发送数据到服务器
	-- 服务器url 127.0.0.1:37012/test/killed
	-- 方法： POST
	-- 参数
	--     steamids 所有玩家的64位steamid的json
	--     duration 击杀所用的秒数，精确到小数点后N位
	--     version 版本号
	--
	-- 服务器接收数据的时候要注意甄别是否发送自VPBot主机的IP
	--
	local request = CreateHTTPRequest("POST",vpgame_server_address .. "killed")
	request:SetHTTPRequestGetOrPostParameter("steamids", JSON:encode(steamids))
	request:SetHTTPRequestGetOrPostParameter("duration", tostring(duration))
	request:SetHTTPRequestGetOrPostParameter("version", version)
	request:Send(function(result)
		if result.StatusCode == 200 then
			-- 发送 “击杀时间已被记录”的信息
			GameRules:SendCustomMessage("#roshan_time_recorded_successed",DOTA_TEAM_GOODGUYS,1)
			-- 告知击杀时间
			GameRules:SendCustomMessage(tostring(duration),DOTA_TEAM_GOODGUYS,1)
			local result_body = JSON:decode(result.Body)
			if result_body.NewRecord then
				-- 发送 “新纪录！”消息
				GameRules:SendCustomMessage("#roshan_time_new_record",DOTA_TEAM_GOODGUYS,1)
			end
		else
			-- 发送 “记录失败，服务器返回了一个错误：[具体错误信息]” 消息
			GameRules:SendCustomMessage("#roshan_time_record_failed_" .. result.StatusCode, DOTA_TEAM_GOODGUYS,1)
		end
		GameRules:SetGameWinner(DOTA_TEAM_GOODGUYS)
	end)
end

-- 游戏状态改变的响应
function RoshanKillerGameMode:OnGameRulesStateChange()

    local newState = GameRules:State_Get()
    if newState == DOTA_GAMERULES_STATE_PRE_GAME then
    	-- 获取进入游戏的时间，这个时间是有英雄进入比赛，开始倒计时的准备时间
    	self.flPreGameTime = GameRules:GetGameTime()
    end

    -- 发送一个请求到服务器，发送本次比赛的ID，同时鉴定服务器是否在线，告知玩家
    -- 服务器url 127.0.0.1:37012/test/killed
	-- 方法： POST
	-- 参数
	--     matchid 本场比赛的ID
	--
	local matchid = GameRules:GetMatchID()

	-- print("Matchid = ", tostring(matchid))

    local request = CreateHTTPRequest("POST", vpgame_server_address .. "start")
    request:SetHTTPRequestGetOrPostParameter("matchid", tostring(matchid))
    request:Send(function(result)
    	if result.StatusCode == 200 then
    		-- 发送 “服务器在线” 消息
			GameRules:SendCustomMessage("#server_online",DOTA_TEAM_GOODGUYS,1)
		else
			-- 发送 “服务器不可用，返回了一个错误：[具体错误]” 信息
			GameRules:SendCustomMessage("#server_unavailable_" .. result.StatusCode, DOTA_TEAM_GOODGUYS,1)
		end
    end)
end

-- 单位被击杀的响应
function RoshanKillerGameMode:OnEntityKilled(keys)
	local killed = EntIndexToHScript(keys.entindex_killed)
	if killed:GetUnitName() == "npc_dota_roshan" then
		self:OnRoshanKilled()
	end
end

-- 初始化
function RoshanKillerGameMode:InitGameMode()
	ListenToGameEvent("game_rules_state_change", Dynamic_Wrap(RoshanKillerGameMode, "OnGameRulesStateChange"), self)
	ListenToGameEvent("entity_killed",Dynamic_Wrap(RoshanKillerGameMode, "OnEntityKilled"),self)
end
